const express = require('express');  //第二次用就是引用之前的缓存
// 创建一个路由
const router = express.Router();

// 引入mysql模块
const db= require("../core/mysql");
// get请求,要拿到前端传输过的数据,使用 query
// post请求,要到前端传输过的数据，使用 body
router.get('/getUsers', async (req, res, next) => {
    let sex = req.query.sex;
    let params = [sex];
    let sql = "select * from users where u_sex=? ;"
    let result = await db.query(sql, params);
    //返回数据 为json,到前端
    res.json(result)

})



//分页的接口
router.get('/getUsersByPage', async (req, res, next) => {
    let pageSize = req.query.pageSize == undefined ? 10 : req.query.pageSize;
    let pageIndex = req.query.pageIndex == undefined ? 1 : req.query.pageIndex;

    let params = [(pageIndex - 1) * parseInt(pageSize), parseInt(pageSize)];
    // console.log(params);
    let sql = "select * from users limit ?,? ;"
    let result = await db.query(sql, params);
    //返回数据 为json,到前端
    res.json({
        code: 200,
        msg: '请求成功',
        data: result
    })

})


module.exports = router;